package cn.kgc.education.controller;

import cn.kgc.education.entity.Course;
import cn.kgc.education.response.ResponseResult;
import cn.kgc.education.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;

/**
 * @author 课工场
 * @date 2024/9/10
 * @description 课程请求处理类
 */
@RestController
@RequestMapping("course")
public class CourseController {

    @Resource
    private CourseService courseService;

    //  课程数据分页接口
    @RequestMapping("page")
    public ResponseResult coursePage(Integer page, Integer limit, String courseNo, String category) {

        Page<Course> coursePage = new Page<>(page, limit);

        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.eq(StringUtils.isNotBlank(courseNo), "course_no", courseNo)
                .eq(StringUtils.isNotBlank(category), "category", category);


        courseService.page(coursePage, courseQueryWrapper);

        ResponseResult responseResult = new ResponseResult();

        responseResult.setCode(0);
        responseResult.setCount(coursePage.getTotal());
        responseResult.setData(coursePage.getRecords());


        return responseResult;
    }


    // 删除课程
    @RequestMapping("delete")
    public ResponseResult deleteCourse(String ids) {

        // 将字符串id  处理成集合
        String[] batchIds = ids.split(",");
        ArrayList<Integer> intIds = new ArrayList<>();
        for (String batchId : batchIds) {
            intIds.add(Integer.valueOf(batchId));
        }

        courseService.removeBatchByIds(intIds);


        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("删除成功");
        return responseResult;

    }


    //  addOrUpdate  添加  修改 课程信息接口
    @RequestMapping("addOrUpdate")
    public ResponseResult addOrUpdate(Course course) {

        ResponseResult responseResult = new ResponseResult();
        //添加时 不允许  课程编号 课程名字重复
        if (course.getId()==null){  // 添加

            QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
            String courseNo = course.getCourseNo();
            String courseName = course.getCourseName();

            //  where  course_no = ?  or course_name = ?
            courseQueryWrapper.eq("course_no",courseNo)
                    .or()
                    .eq("course_name",courseName);

            Course one = courseService.getOne(courseQueryWrapper);

            if (ObjectUtils.isNotNull(one)){  //  判定添加的课程的是否存在   存在
                    responseResult.setCode(110);
                    responseResult.setMsg("课程编号或课程名称重复");
                    return  responseResult;
            }

            course.setCreateTime(new Date());

        }

        courseService.saveOrUpdate(course);

        responseResult.setCode(0);
        responseResult.setMsg("操作成功");
        return  responseResult;

    }


}
